﻿/**************************************************************
 *
 * 命名空间：Sgr.DataCategories.Infrastructure.EntityConfigurations
 * 创建时间：2025/3/19
 * 描述：行政区划实体配置
 *
 **************************************************************/

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Sgr.Data;
using Sgr.DataCategories.Domain.AdministrativeDivisions;
using Sgr.EntityFrameworkCore.EntityConfigurations;

namespace Sgr.DataCategories.Infrastructure.EntityConfigurations
{
    /// <summary>
    /// 行政区划实体类型配置
    /// </summary>
    internal class AdministrativeDivisionEntityTypeConfiguration : EntityTypeConfigurationBase<AdministrativeDivision, long>
    {
        public AdministrativeDivisionEntityTypeConfiguration(DatabaseType? databaseType = null) : base(databaseType)
        { }

        public override void Configure(EntityTypeBuilder<AdministrativeDivision> builder)
        {
            builder.ToTable("sgr_administrative_division");

            base.Configure(builder);

            // 配置基本字段
            builder.PropertyAndHasColumnName(b => b.Code, GetColumnNameCase())
                .HasMaxLength(100)
                .IsRequired()
                .HasComment("行政区划代码");

            builder.PropertyAndHasColumnName(b => b.Name, GetColumnNameCase())
                .HasMaxLength(200)
                .IsRequired()
                .HasComment("行政区划名称");

            builder.PropertyAndHasColumnName(b => b.Level, GetColumnNameCase())
                .IsRequired()
                .HasComment("行政区划级别：0-未知，1-国家，2-省，3-市，4-区县，5-乡镇街道");

            builder.PropertyAndHasColumnName(b => b.ShortName, GetColumnNameCase())
                .HasMaxLength(100)
                .HasComment("行政区划简称");

            builder.PropertyAndHasColumnName(b => b.PinyinName, GetColumnNameCase())
                .HasMaxLength(255)
                .HasComment("拼音名称");

            builder.PropertyAndHasColumnName(b => b.PinyinAbbr, GetColumnNameCase())
                .HasMaxLength(255)
                .HasComment("拼音缩写");

            builder.PropertyAndHasColumnName(b => b.Description, GetColumnNameCase())
                .HasMaxLength(1000)
                .HasComment("行政区划描述");

            builder.PropertyAndHasColumnName(b => b.State, GetColumnNameCase())
                .IsRequired()
                .HasComment("状态：0-启用，1-禁用");

            builder.PropertyAndHasColumnName(b => b.OrderNumber, GetColumnNameCase())
                .HasDefaultValue(0)
                .HasComment("排序号");

            // 配置树形结构字段
            builder.PropertyAndHasColumnName(b => b.ParentId, GetColumnNameCase())
                .IsRequired()
                .HasComment("上级行政区划ID");

            builder.PropertyAndHasColumnName(b => b.NodePath, GetColumnNameCase())
                .HasMaxLength(1000)
                .IsRequired()
                .HasComment("节点路径");

            // 添加索引
            // 树形结构索引
            base.AddTreeNodeIndex(builder);

            // 代码唯一索引
            builder.HasIndex(b => b.Code).IsUnique().HasDatabaseName("UK_AdminDiv_Code");

            // 名称索引
            builder.HasIndex(b => b.Name).HasDatabaseName("IX_AdminDiv_Name");
        }
    }
}